import 'dart:convert';

import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:lcapp/api/interface/message.dart';
import 'package:lcapp/api/request/response_data.dart';
import 'package:lcapp/data/message_data_entity.dart';
import 'package:lcapp/model/refresh/refresh.dart';
import 'package:lcapp/tools/date_util.dart';
import 'package:lcapp/widget/asr_app_bar.dart';
import 'package:lcapp/widget/asr_cache_image.dart';

class FaxianHudongPage extends StatefulWidget {
  const FaxianHudongPage({Key? key}) : super(key: key);

  @override
  _FaxianHudongPageState createState() => _FaxianHudongPageState();
}

class _FaxianHudongPageState extends State<FaxianHudongPage> {

  int pageNum = 1;
  int pageSize = 10;

  List<MessageItem> messages = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getList();
  }

  getList() async {
    ResponseData res = await MessageAPI.getMessageList(pageNum, pageSize,2);
    if (res.code == 200) {
      if(pageNum==1){
        messages.clear();
      }
      List arr = res.data??[];
      for (var element in arr) {
        MessageItem item = MessageItem().fromJson(element);
        messages.add(item);
      }
    }
    if(mounted){setState(() {
    });}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AsrAppBar(
        titleText: '互动消息',
      ),
      body: Container(
        child: EasyRefresh(
          header: myRefreshHeader(),
          footer: myRefreshFooter(),
          onRefresh: ()async{
            pageNum=1;
            getList();
          },
          onLoad: ()async{
            pageNum++;
            getList();
          },
          child: ListView.builder(
              itemCount: messages.length,
              itemBuilder: (context,index){
                MessageItem item = messages[index];
                Map<String,dynamic> exta = jsonDecode(item.exta!);
                return GestureDetector(
                  onTap: (){
                    Navigator.of(context).pushNamed("ShopHomePage",arguments: item.fromUserId);
                  },
                  child: Container(
                    color: Colors.transparent,
                    height: 140.w,
                    padding: EdgeInsets.symmetric(horizontal: 30.w),
                    margin: EdgeInsets.only(bottom: 40.w),
                    child: Column(
                      children: [
                        Expanded(child: Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          children: [
                            ClipRRect(
                              child:ASRCacheImage(width: 90.w,height:90.w,fit: BoxFit.fitWidth,image: exta['headImg']??'assets/icon/p_head.png',),
                              borderRadius: BorderRadius.circular(90.w),
                            ),
                            SizedBox(width: 30.w,),
                            Expanded(child: Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              crossAxisAlignment: CrossAxisAlignment.start,
                              children: [
                                Text(item.title??'',
                                  style: TextStyle(
                                      color: Colors.black,
                                      fontSize: 32.sp,
                                      fontWeight: FontWeight.w600
                                  ),),
                                SizedBox(height: 10.w,),
                                Text(item.content??'',style: TextStyle(
                                  color: Color(0xff848484),
                                  fontSize: 28.sp,
                                ))
                              ],
                            )),
                            Column(
                              mainAxisAlignment: MainAxisAlignment.center,
                              children: [
                                Text(DateUtil.getDateScope(checkDate: item.createTime),style: TextStyle(
                                  color: Color(0xff848484),
                                  fontSize: 26.sp,
                                )),
                              ],
                            )
                          ],
                        )),
                        Container(
                          height: 1,
                          // margin: EdgeInsets.only(left: 120.w),
                          color: Color(0xffE9E8E9),
                        ),
                      ],
                    ),
                  ),
                );
          }),
        ),
      ),
    );
  }
}
